package Presentation;

import BusinessLogic.*;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public class frmFindFriend extends javax.swing.JFrame {
    public DefaultListModel model1 = new DefaultListModel();
    public String name="";

    public frmFindFriend() {
        initComponents();
        ls_userfind.setModel(model1);
        ls_userfind.enable(false);
        lb_notify.setVisible(false);
        setIcon();
    }
    
    private void setIcon() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/icon.png")));
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txt_user = new javax.swing.JTextField();
        btn_find = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ls_userfind = new javax.swing.JList();
        btn_chat = new javax.swing.JButton();
        btn_addfriend = new javax.swing.JButton();
        lb_notify = new javax.swing.JLabel();

        setTitle("Tìm bạn");
        setResizable(false);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 0, 0));
        jLabel4.setText("G_IT");

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jLabel1.setText("Nhập tên User bạn muốn tìm");

        txt_user.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_userKeyPressed(evt);
            }
        });

        btn_find.setBackground(new java.awt.Color(204, 204, 255));
        btn_find.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_find.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/search.png"))); // NOI18N
        btn_find.setText("Tìm Bạn");
        btn_find.setToolTipText("Tìm bạn");
        btn_find.setBorder(null);
        btn_find.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_findActionPerformed(evt);
            }
        });

        ls_userfind.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        ls_userfind.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { " " };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        ls_userfind.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ls_userfindMouseClicked(evt);
            }
        });
        ls_userfind.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                ls_userfindValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(ls_userfind);

        btn_chat.setBackground(new java.awt.Color(204, 204, 255));
        btn_chat.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_chat.setText("Chat");
        btn_chat.setBorder(null);
        btn_chat.setEnabled(false);
        btn_chat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_chatActionPerformed(evt);
            }
        });

        btn_addfriend.setBackground(new java.awt.Color(204, 204, 255));
        btn_addfriend.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        btn_addfriend.setText("Thêm bạn");
        btn_addfriend.setBorder(null);
        btn_addfriend.setEnabled(false);
        btn_addfriend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_addfriendActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_addfriend, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_chat, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(btn_addfriend, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btn_chat, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lb_notify.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        lb_notify.setForeground(new java.awt.Color(255, 0, 0));
        lb_notify.setText("thông báo");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lb_notify)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(txt_user, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addComponent(jLabel4))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btn_find, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_user, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_find, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addComponent(lb_notify)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getAccessibleContext().setAccessibleParent(this);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txt_userKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_userKeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            btn_findActionPerformed(null);
        }
    }//GEN-LAST:event_txt_userKeyPressed

    private void btn_findActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_findActionPerformed
        if(!txt_user.getText().equals("")){
           model1.clear();
           lb_notify.setVisible(false);
           ls_userfind.setEnabled(true);
           try{
               sendData(Command.FIND_REQUEST+txt_user.getText());
           }
           catch(Exception e){}
           txt_user.setText("");
           txt_user.requestFocus();
        }
        else{
            model1.clear();
            txt_user.requestFocus();
            lb_notify.setText("Phải nhập vào ô tìm kiếm!");
            lb_notify.setVisible(true);
        }       
    }//GEN-LAST:event_btn_findActionPerformed

    private void ls_userfindValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_ls_userfindValueChanged
        if(!ls_userfind.getSelectedValue().toString().equals("")){
            btn_chat.setEnabled(true);
            btn_addfriend.setEnabled(true);
        }
        else{
            btn_chat.setEnabled(false);
            btn_addfriend.setEnabled(false);
        }
    }//GEN-LAST:event_ls_userfindValueChanged

    private void btn_addfriendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_addfriendActionPerformed
        try{
            sendData(Command.ADD_FRIEND_REQUEST+name+((char)001)+ls_userfind.getSelectedValue().toString());
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(rootPane, "Lỗi thêm bạn", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btn_addfriendActionPerformed

    private void btn_chatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_chatActionPerformed
        try{
            sendData(Command.CHAT_FRIEND_REQUEST+name+((char)001)+ls_userfind.getSelectedValue().toString());
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(rootPane, "Không thể chat", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btn_chatActionPerformed

    private void ls_userfindMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ls_userfindMouseClicked
        if(evt.getClickCount()==2){
            try{
                sendData(Command.CHAT_FRIEND_REQUEST+name+((char)001)+ls_userfind.getSelectedValue().toString());
            }
            catch(Exception e){}
        }
    }//GEN-LAST:event_ls_userfindMouseClicked

    public void setNotify(){
        lb_notify.setText("Không tìm thấy tên cần tìm!");
        lb_notify.setVisible(true);
    }
    
    public void listFind(String ds){
        lb_notify.setVisible(false);
        int spos=0,epos=-1;
        String find;
        for(int i=0;i<ds.length();i++){
            if(ds.charAt(i)==001){
                spos=epos+1;
                epos=i;
                find=new String(ds.substring(spos,epos));
                if(!find.equals(name)){
                    model1.addElement(find);
                }
            }
        }
    }
    
    private void sendData(String str){
        if(!Connection.sendData(str)){
            JOptionPane.showMessageDialog(rootPane,"Lỗi! Mất kết nối với server", "Thông kết nối", JOptionPane.ERROR_MESSAGE);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_addfriend;
    private javax.swing.JButton btn_chat;
    private javax.swing.JButton btn_find;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lb_notify;
    private javax.swing.JList ls_userfind;
    private javax.swing.JTextField txt_user;
    // End of variables declaration//GEN-END:variables
}
